package com.igd.socket.sdk.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class BIOSocket {
	
	public void start() throws IOException {
		ServerSocket serverSocket = new ServerSocket(10020);
		Socket socket = serverSocket.accept(); // 阻塞获取客户端连接
		// 启动新的线程操作客户端的读数据或写数据
		new Thread(() -> {
			try {
				byte[] data = new byte[1024];
				InputStream clientIns = socket.getInputStream();
				clientIns.read(data);
				// 本来这步需要解码
				String reqMsg = new String(data, StandardCharsets.UTF_8);
				System.out.println("get req：" + reqMsg);
				OutputStream clientOuts = socket.getOutputStream();
				String rspMsg = "resp：" + reqMsg;
				clientOuts.write(rspMsg.getBytes(StandardCharsets.UTF_8));
				clientOuts.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}).start();
	}
	

}
